package com.csea;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Csea
 * @title
 * @date 2019/12/12 19:22
 */
public class Test {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("tom", "男", 16));
        personList.add(new Person("jerry", "男", 18));
        personList.add(new Person("mary", "男", 24));
        personList.add(new Person("tony", "男", 30));
        personList.add(new Person("csea", "男", 22));
        personList.add(new Person("spack", "男", 50));


//        匿名内部类实现方式
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println(personList);

//        lambda表达式 实现
        Collections.sort(personList, (p1, p2) -> p1.getAge() - p2.getAge());
        System.out.println(personList);

//        静态方法引用
        Collections.sort(personList, Person::compareByAge);
        System.out.println(personList);

//        实例方法引用方式
        PersonUtil personUti = new PersonUtil();
        Collections.sort(personList, personUti::compareByName);
        System.out.println(personList);

//        构造方法引用：函数式接口使用
        IPerson ip = Person::new;
        Person person = ip.initPerson("tom", "男", 22);
        System.out.println(person);

    }

}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private String gender;
    private int age;

    public static int compareByAge(Person person1, Person person2) {
        return person1.getAge() - person2.getAge();
    }
}

class PersonUtil {
    public int compareByName(Person p1, Person p2) {
        return p1.getName().hashCode() - p2.getName().hashCode();
    }
}

interface IPerson {
    Person initPerson(String name, String gender, int age);
}
